// 用户信息
var userInfo = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user;

// 客户组ID
var mainCustomerId = sessionStorage.getItem("mainCustomerId")

//页大小
var pageSize = 50;

/*账号对接失败列表分页*/

$(function () {
    //绑定所属渠道下拉列表
    accountFailedPageList();
})

//账号对接失败列表分页-列表
function accountFailedPageList () {
    var json = {
        pageNo: 1,
        pageSize: pageSize
    }
    $.ajax({
        url: root.accountFailed.syncFailList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(json),
        success: function (result) {
            accountFailed_Shield(result);
            $('.M-box3-accountFailed').pagination({
                pageCount: result.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    json.pageNo = api.getCurrent();
                    $.ajax({
                        url: root.accountFailed.syncFailList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType: 'json',
                        contentType: "application/json",
                        data: JSON.stringify(json),
                        success: function (result) {
                            accountFailed_Shield(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}

//账号对接失败-渲染列表+分页
function accountFailed_Shield (dataSource) {
    var dttable = $('#shanDataTable').dataTable(); // 初始化dataTable，清空上一个值
    dttable.fnClearTable(); //清空一下table
    dttable.fnDestroy(); //还原初始化了的datatable
    $("#shanDataTable").DataTable({
        "order": [[2, "desc"]],
        "autoWidth": false,
        "ordering": false,
        "searching": false,
        "paging": false,
        "retrieve": true,
        "destroy": true,
        "bAutoWidth": true,
        "bLengthChange": true,
        "aLengthMenu": [5, 10, 25, 50],
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "抱歉， 没有找到",
            "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
            "sInfoEmpty": "",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            },
            "sZeroRecords": "没有检索到数据",
            "sProcessing": "<img src='./loading.gif' />",
        },
        data: dataSource.data.data,
        columnDefs: [
            {
                "render": function (data, type, row, meta) {
                    var val = row.mainCustomerCode 
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 0
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.mainCustomerName 
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 1
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.module 
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 2
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.loginAccount 
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 3
            },
            {
                "render": function (data, type, row) {
                    var val = row.failReason
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 4
            },
            {
                "render": function (data, type, row) {
                    var val = "<a href='javascript:0;' onclick=\"retrySync(" + row.id + ")\">重新生成</a> ";
                    return val;
                },
                "targets": 5
            },
        ]
    });
};

//单条重新生成
function retrySync(id)
{
    param.idList = [id];
    $.ajax({
        url: root.accountFailed.retrySync,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(json),
        success: function (result) {
            $.Huimodalalert(result.msg, 2000);
        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}

//全部重新生成
function retrySyncAll()
{
    $.ajax({
        url: root.accountFailed.retrySyncAll,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        success: function (result) {
            $.Huimodalalert(result.msg, 2000);
        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}
